package com.l2jfrozen.gameserver.model.entity;

import javolution.util.FastList;

import com.l2jfrozen.gameserver.model.entity.Announcements;
import com.l2jfrozen.gameserver.model.actor.instance.L2PcInstance;
import com.l2jfrozen.util.random.Rnd;

/**
 * @author Marwan
 */
public class Hitman
{
       @SuppressWarnings("unused")
       public static FastList<L2PcInstance> _players = new FastList<L2PcInstance>();
       public static L2PcInstance chossenPlayer = null;
      
       public static enum State
       {
               INACTIVE,
               ACTIVE
       }
      
       public static State state = State.INACTIVE;
      
       public class Start implements Runnable
       {
               @Override
               public void run()
               {
                       startEvent();
               }
       }
      
       public void startEvent()
       {
               Announcements.getInstance().announceToAll("Hitman event started.");
               Announcements.getInstance().announceToAll("Player will be chossen in 30 seconds to be the hitman.");
               pickplayer();
               waitSecs(30);
               announce();
               chosse(chossenPlayer);
              
               if (state == State.ACTIVE)
               {
                       Announcements.getInstance().announceToAll("Hitman Event will end in 5 minutes.");
                       wait(4);
                      
                       Announcements.getInstance().announceToAll("Hitman Event will end in 1 minutes.");
                       wait(1);
                       end();
               }
              
       }
      
       public void pickplayer()
       {
               state = State.ACTIVE;
               chossenPlayer = _players.get(Rnd.get(0, _players.size() - 1));
       }
      
       public void end()
       {
               Announcements.getInstance().announceToAll("No one killed the Hitman");
               Announcements.getInstance().announceToAll("Hitman event has end");
               _players.clear();
              
       }
      
       public void announce()
       {
               Announcements.getInstance().announceToAll("Player :" + chossenPlayer.getName() + " has been chossen");
               Announcements.getInstance().announceToAll("The first who kills him wil be rewarded");
               Announcements.getInstance().announceToAll("Event will end in 10 minutes.");
       }
      
       public void chosse(L2PcInstance pl)
       {
               pl.setHitman(true);
               pl.setTitle("HitMan Target");
               pl.getAppearance().setTitleColor(255, 0, 0);
       }
      
       public void rewardWinner(L2PcInstance a)
       {
               a.addItem("hitman", 9999, 125, a, false); //reward
       }
      
       public void annoucneWinner(L2PcInstance a)
       {
              
               Announcements.getInstance().announceToAll(a.getName() + " has won Hitman Event");
              
       }
      
       public void clear()
       {
               _players.clear();
       }
      
       public void waitSecs(int i)
       {
               try
               {
                       Thread.sleep(i * 1000);
               }
               catch (InterruptedException ie)
               {
                       ie.printStackTrace();
               }
       }
      
       public void wait(int i)
       {
               try
               {
                       Thread.sleep(i * 60000);
               }
               catch (InterruptedException ie)
               {
                       ie.printStackTrace();
               }
       }
      
       public Hitman()
       {
               // ThreadPoolManager.getInstance().scheduleAiAtFixedRate(new Start(), 600000, 600000);
       }
      
       public static Hitman getInstance()
       {
               return SingletonHolder._instance;
       }
      
       private static class SingletonHolder
       {
               @SuppressWarnings("synthetic-access")
               protected static final Hitman _instance = new Hitman();
       }
}